package Controllers;

import Bean.BeanClass;
import Impl.BookServiceImpl;
import Impl.ClassServiceImpl;
import Service.ClassService;
import com.alibaba.fastjson.JSONArray;
import exception.BookNotFoundException;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Servlet implementation class BookDetailsController
 */
@Controller
public class ClassDetailsController extends HttpServlet {
	private static final long serialVersionUID = 1L;

    private ClassServiceImpl classService;

    @RequestMapping("/classdetails")
    public void handleRequest(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
    	response.setContentType("text/html;charset=UTF-8");  
    	JSONArray arr = new JSONArray();
    	ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
        classService=(ClassServiceImpl)applicationContext.getBean("ClassServiceImpl");
        BeanClass myclass;
		String aString='"'+"";
        int classId=Integer.parseInt(request.getParameter("classId").replaceAll(aString, ""));
        System.out.println(classId);
		try {
			myclass = classService.getClassDetails(classId);
			System.out.println(myclass.getClass_id());
			arr.add(myclass.toJson());
		} catch (BookNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

//        if (request.getParameter("bookId") != null) {
//            try {
//            	int bookId=Integer.parseInt(request.getParameter("bookId"));
//            	System.out.println(bookId);
//                bd = bookService.getBookDetails(bookId);
//                arr.add(bd.toJson());
//            } catch (BookNotFoundException ex) {
//                throw new ServletException(ex);
//            }
//
//        }
        response.getWriter().append(arr.toJSONString());
//        request.getRequestDispatcher("index/bookdetail.jsp").forward(request,response);
    }
}
